from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from django.contrib.contenttypes.admin import GenericTabularInline
from .models.log import OperationLog
from .models.user_profile import UserProfile
from .models import CoursePeriod, ClassCourse

@admin.register(CoursePeriod)
class CoursePeriodAdmin(admin.ModelAdmin):
    list_display = ('name', 'start_time', 'duration', 'end_time')
    search_fields = ('name',)
    list_filter = ('start_time',)
    
    def end_time(self, obj):
        return obj.end_time
    end_time.short_description = '结束时间'

@admin.register(OperationLog)
class OperationLogAdmin(admin.ModelAdmin):
    list_display = ('user', 'operation_type', 'content_type', 'object_name', 'timestamp', 'ip_address')
    list_filter = ('operation_type', 'content_type', 'timestamp')
    search_fields = ('user__username', 'object_name', 'details')
    readonly_fields = ('user', 'operation_type', 'content_type', 'object_id', 'object_name', 'timestamp', 'ip_address', 'details')

    def has_add_permission(self, request):
        return False

    def has_change_permission(self, request, obj=None):
        return False

    def has_delete_permission(self, request, obj=None):
        return False

from .models import ClassEvaluation
admin.site.register(ClassEvaluation)

from .models import BaseModel, SchoolYear, Semester, Role, UserRoleAssignment, Grade, ClassInfo, Subject, TeacherSubjectAssignment, ClassSubjectAssignment, Student, StudentClassEnrollment, StudentClassRelationship, DailyAttendance, LeaveRequest, Homework, HomeworkSubmission,  Report, GradeSchedule, ClassroomEvaluation, StudentMisbehavior

admin.site.register(SchoolYear)
admin.site.register(Semester)
admin.site.register(Role)
admin.site.register(UserRoleAssignment)
admin.site.register(Grade)
admin.site.register(ClassInfo)
admin.site.register(Subject)
admin.site.register(TeacherSubjectAssignment)
admin.site.register(ClassSubjectAssignment)
admin.site.register(Student)
admin.site.register(StudentClassEnrollment)
admin.site.register(HomeworkSubmission)
admin.site.register(Homework)
admin.site.register(ClassroomEvaluation)
admin.site.register(StudentMisbehavior)
admin.site.register(ClassCourse)

class UserProfileInline(admin.StackedInline):
    model = UserProfile
    can_delete = False
    verbose_name_plural = '用户信息'
    fields = ()

class CustomUserAdmin(UserAdmin):
    # 自定义列表显示字段
    list_display = ('get_real_name', 'email', 'first_name', 'last_name', 'is_staff', 'is_active', 'date_joined')
    # 自定义搜索字段
    search_fields = ('profile__real_name', 'email', 'first_name', 'last_name')
    # 自定义筛选器
    list_filter = ('is_staff', 'is_active', 'date_joined', 'groups')
    # 自定义详情页字段集
    fieldsets = (
        (None, {'fields': ('username', 'password')}),
        ('权限', {'fields': ('is_active', 'is_staff', 'is_superuser', 'groups', 'user_permissions')}),
    )
    inlines = (UserProfileInline,)
    
    def get_real_name(self, obj):
        return obj.profile.real_name if hasattr(obj, 'profile') else obj.username
    get_real_name.short_description = '真实姓名'

# 先取消注册默认的User模型
admin.site.unregister(User)
# 注册自定义的用户管理
admin.site.register(User, CustomUserAdmin)
